﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MathQ
{
    public partial class Clock : UserControl
    {
        private int timerCount;
        private bool ifInAlert;

        public Clock()
        {
            InitializeComponent();
        }

        public void Start()
        {
            pictureBox_clock.Image = (Image)Program._resources.GetObject("clock_");
            Color normalColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
            label_mm.ForeColor = normalColor;
            label_ss.ForeColor = normalColor;
            label_t.ForeColor = normalColor;
            label_mm.Text = "00";
            label_ss.Text = "00";

            timer1.Interval = 1000;
            timer1.Enabled = true;
            timer1.Start();

            timerCount = 0;
            ifInAlert = false;
        }

        public void Stop()
        {
            timer1.Stop();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            timerCount++;

            int mm = Convert.ToInt32(Math.Floor(Convert.ToDouble(timerCount / 60)));
            label_mm.Text = mm.ToString();
            if (label_mm.Text.Length < 2) label_mm.Text = "0" + label_mm.Text;

            int ss = timerCount - mm * 60;
            label_ss.Text = ss.ToString();
            if (label_ss.Text.Length < 2) label_ss.Text = "0" + label_ss.Text;

            if (ifInAlert == false && timerCount > 800)
            {
                ifInAlert = true;
                pictureBox_clock.Image = (Image)Program._resources.GetObject("clock_red");
                Color alertColor = Color.OrangeRed;
                label_mm.ForeColor = alertColor;
                label_ss.ForeColor = alertColor;
                label_t.ForeColor = alertColor;
            }
        }


        public void Resume()
        {
            if (timer1.Enabled == false) timer1.Enabled = true;
            timer1.Start();
        }


        public void Reset()
        {
            if (timer1.Enabled == true) timer1.Enabled = false;
            pictureBox_clock.Image = (Image)Program._resources.GetObject("clock_");
            Color normalColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
            label_mm.ForeColor = normalColor;
            label_ss.ForeColor = normalColor;
            label_t.ForeColor = normalColor;
            label_mm.Text = "00";
            label_ss.Text = "00";
        }
    }
}
